import { createI18n, useI18n } from 'vue-i18n'
import enUS from './en-US'
import zhCN from './zh-CN'

const messages = {
  'en-US': enUS,
  'zh-CN': zhCN,
}

export type KeyofLocale = keyof typeof messages

export const LOCALE_OPTIONS = [
  { label: '中文', value: 'zh-CN' },
  { label: 'English', value: 'en-US' },
]

const defaultLocale = useStorage<KeyofLocale>('locale', 'en-US') // 默认语言

export const i18n = createI18n({
  legacy: false, // 使用组合式API
  locale: defaultLocale.value,
  fallbackLocale: 'en-US',
  messages,
})

// 切换语言的方法
export function setLocale(locale: KeyofLocale) {
  i18n.global.locale.value = locale
}

// const { t } = useI18n()

export const T = (token: string) => {
  const { t } = useI18n()
  return t(token)
}

export const localeGetter = () => {
  const { locale } = useI18n()
  return locale
}
